package com.stylefeng.guns.modular.system.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.common.constant.state.KpMethod;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpsClient;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.BkStatsMapper;
import com.stylefeng.guns.modular.system.model.BkStats;
import com.stylefeng.guns.modular.system.model.Gpdata;
import com.stylefeng.guns.modular.system.model.Params;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Hashtable;

@Service
public class JxBkImportTask {

	@Autowired
	private BkStatsMapper bkStatsMapper;

	public void execute(boolean isHoliday) {
		if(isHoliday){
			Hashtable<String, String> paramsMaps = new Hashtable<String, String>();
			paramsMaps.put("a", KpMethod.APP_B.getMethod());
			paramsMaps.put("c", "ZhiShuRanking");
			paramsMaps.put("apiv", "w13");
			paramsMaps.put("ZSType", "7");
			paramsMaps.put("Type", "1");
			paramsMaps.put("PhoneOSNew", "2");
			paramsMaps.put("Order", "1");
			paramsMaps.put("Index", "0");
			paramsMaps.put("Date", DateUtil.getDays());
			paramsMaps.put("st", "10");
			String result = HttpsClient.postSSL(ImportContants.getBkKpDataPathStr(), paramsMaps,false);
			if(StringUtils.isNotEmpty(result)){
				JSONObject jsonObj = JSON.parseObject(result);
				if(jsonObj != null){
					JSONArray jsonArray = JSON.parseArray(jsonObj.getString("list"));
					for(int i=0;i<jsonArray.size();i++){
						JSONArray entity = (JSONArray) jsonArray.get(i);

						BkStats bkStats = new BkStats();
						bkStats.setgBkCode(Integer.valueOf(entity.get(0).toString()));
						bkStats.setgBkDate(new Date());

						bkStats = bkStatsMapper.selectByDate(bkStats);
						if(bkStats == null){
							bkStats = new BkStats();
							bkStats.setgBkCode(Integer.valueOf(entity.get(0).toString()));
							bkStats.setgBkDate(new Date());
							bkStats.setgName(entity.get(1).toString());
							bkStats.setgBkQd(Integer.valueOf(entity.get(2).toString()));
							bkStatsMapper.insert(bkStats);
						}else{
							bkStats.setgBkQd(Integer.valueOf(entity.get(2).toString()));
							bkStatsMapper.updateById(bkStats);
						}

					}
				}
			}
		}
	}
}
